//
//  SwipeableViewController.m
//  oschina
//
//  Created by 如佳 陈 on 15/6/24.
//  Copyright (c) 2015年 crj. All rights reserved.
//

#import "SwipeableViewController.h"

#define TITLE_TAG_START 100
#define INDICATOR_HEIGHT 2

@interface SwipeableViewController ()<UIScrollViewDelegate>{
    CGFloat _titlesViewHeight;
    CGFloat _titlesViewWidth;
}
@property (nonatomic, retain) UIView *titlesView;
@property (nonatomic, retain) UIView *indicator;

@end

@implementation SwipeableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titlesViewHeight = 36.0;
    _titlesViewWidth = self.view.frame.size.width;
    self.tagBarTintColor = [UIColor blackColor];
    self.index = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.titlesView];
    [self.view addSubview:self.contentView];
    
//    self.view.translatesAutoresizingMaskIntoConstraints = NO;
//    self.titlesView.translatesAutoresizingMaskIntoConstraints = NO;
//    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
//    NSDictionary *views = NSDictionaryOfVariableBindings(self.view, self.titlesView, self.contentView);
//    [NSLayoutConstraint constraintsWithVisualFormat:@"" options:0 metrics:nil views:views];
}

- (void)setTitles:(NSArray *)titles{
    _titles = titles;
    for (NSInteger i = 0; i < _titles.count; i++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = _titles[i];
        label.tag = TITLE_TAG_START + i;
        label.userInteractionEnabled = YES;
        label.frame = CGRectMake(i * _titlesViewWidth/_titles.count, 0, _titlesViewWidth/_titles.count, _titlesViewHeight);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = self.tagBarTextColor;
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [label addGestureRecognizer:tap];
        if (self.index == i) {
            label.textColor = self.tagBarTintColor;
        }
        [self.titlesView addSubview:label];
    }
    self.indicator.frame = CGRectMake(0, _titlesViewHeight - INDICATOR_HEIGHT, _titlesViewWidth/_titles.count, INDICATOR_HEIGHT);
    [_titlesView addSubview:self.indicator];
}

- (void)tapAction:(UITapGestureRecognizer*)ges{
    self.index = [ges view].tag - TITLE_TAG_START;
}

#pragma mark - index

- (void)setIndex:(NSInteger)index{
    _index = index;
    for (NSInteger i = 0 ; i < self.titles.count; i++) {
        if (i == _index) {
            ((UILabel*)[self.view viewWithTag:i + TITLE_TAG_START]).textColor = self.tagBarTintColor;
        }else{
            ((UILabel*)[self.view viewWithTag:i + TITLE_TAG_START]).textColor = self.tagBarTextColor;
        }
    }
}

- (UIView *)titlesView{
    if (!_titlesView) {
        _titlesView = [[UIView alloc]init];
        _titlesView.backgroundColor = [UIColor grayColor];
        _titlesView.frame = CGRectMake(0, 0, _titlesViewWidth, _titlesViewHeight);
        _titlesView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    }
    return _titlesView;
}

- (void)setTagBarTextColor:(UIColor *)tagBarTextColor{
    _tagBarTextColor = tagBarTextColor;
    for (NSInteger i = 0 ; i < self.titles.count; i++) {
        if (i == self.index) {
            continue;
        }
        UILabel *label = (UILabel*)[self.view viewWithTag:i + TITLE_TAG_START];
        label.textColor = _tagBarTextColor;
    }
}

- (void)setTagBarTintColor:(UIColor *)tagBarTintColor{
    _tagBarTintColor = tagBarTintColor;
    self.indicator.backgroundColor = tagBarTintColor;
}

- (void)setTagBarBackgroundColor:(UIColor*)color{
    self.titlesView.backgroundColor = color;
}

- (UIScrollView *)contentView{
    if (!_contentView) {
        BOOL hasTabBar = !self.hidesBottomBarWhenPushed;
        _contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _titlesViewHeight, _titlesViewWidth, self.view.frame.size.height - 64 - _titlesViewHeight - (hasTabBar?49:0))];
//        _contentView = [[UIScrollView alloc]init];
        _contentView.pagingEnabled = YES;
        _contentView.showsHorizontalScrollIndicator = NO;
        _contentView.delegate = self;
        _contentView.bounces = NO;
        _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    }
    return _contentView;
}

- (void)setViewControllers:(NSArray *)viewControllers{
    _viewControllers = viewControllers;
    self.contentView.contentSize = CGSizeMake(self.contentView.frame.size.width * _viewControllers.count, self.contentView.frame.size.height);
    for (NSInteger i = 0; i < _viewControllers.count; i++) {
        UIView *view = ((UIViewController*)_viewControllers[i]).view;
        view.frame = CGRectMake(self.contentView.frame.size.width * i, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
        [self.contentView addSubview:view];
    }
}

- (UIView *)indicator{
    if (!_indicator) {
        _indicator = [[UIView alloc]init];
        _indicator.backgroundColor = self.tagBarTintColor;
    }
    return _indicator;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat ratio = scrollView.contentOffset.x/scrollView.contentSize.width;
    CGRect frame = self.indicator.frame;
    
    if (ratio*_viewControllers.count == (NSInteger)(ratio*_viewControllers.count)) {
        self.index = ratio * _viewControllers.count;
//        NSLog(@"%ld",self.index);
    }
    self.indicator.frame = CGRectMake(ratio*_titlesViewWidth, frame.origin.y, frame.size.width, frame.size.height);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat ratio = scrollView.contentOffset.x/scrollView.contentSize.width;
    self.index = ratio * _viewControllers.count;
}

@end
